package com.zhw.mybatis.sqlSession;

import com.zhw.mybatis.config.ZhwConfiguration;
import com.zhw.mybatis.executor.ZhwExecutor;
import com.zhw.mybatis.mapper.MapperProxy;

import java.lang.reflect.Proxy;

public class ZhwSqlSession {

    private ZhwConfiguration configuration;

    private ZhwExecutor executor;

    /**
     * 默认构造方法
     * @param configuration
     */
    public ZhwSqlSession(ZhwConfiguration configuration,ZhwExecutor executor) {
        this.configuration = configuration;
        this.executor = executor;
    }

    /**
     * get() set()
     */

    public ZhwConfiguration getConfiguration() {
        return configuration;
    }

    public void setConfiguration(ZhwConfiguration configuration) {
        this.configuration = configuration;
    }

    public ZhwExecutor getExecutor() {
        return executor;
    }

    public void setExecutor(ZhwExecutor executor) {
        this.executor = executor;
    }


    /**
     * 查询
     * @param statement (sql)
     * @param args 请求参数
     * @param <T> 数据库返回的数据
     * @return
     */
    public <T> T selectOne(String statement,Object[] args,Class returnType){
        return executor.query(statement,args,returnType);
    }

    /**
     * 获取Mapper方法
     * @param clazz 传入的是接口
     * @param <T> 要代理的对象
     * @return 生成的代理对象
     */
   public <T> T getMapper(Class<T> clazz){
       return (T)Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, new MapperProxy(this,clazz));
    }


}
